<!DOCTYPE html>
<html>

<head>
  <meta charset= "KOI8-R">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="google" value="notranslate">
  <link rel="shortcut icon" href="../static/scratch/static/favicon.ico">
  <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
  <title>氧化钙 | 编辑器</title>
  <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  <span id="getId" data-username="{{ username }}" data-is_share="{{ is_share }}" data-project_id = "{{ project_id }}" data-is_author = "{{ is_author }}" adata-project_name = "{{ project_name }}" data-projectfile = ""></span> 
  <script> 
  const getId = document.getElementById('getId'); 
  const username_dis = getId.getAttribute("data-username"); 
  const project_id = getId.getAttribute("data-project_id");
  const project_name = getId.getAttribute("data-project_name");
  var is_share = getId.getAttribute("data-is_share");
  if (is_share==1){
          is_share = true
        }else{
          is_share = false
        }
  var is_author = getId.getAttribute("data-is_author");
  if (is_author==1){
          is_author = true
        }else{
          is_author = false
        }
  var projectCover;
  var projectfile;
  var projectName;
  var data_now;
    window.scratchConfig = {
      logo: {
        show: true, //是否显示
        url: "../static/scratch/static/logo.png", //logo地址，支持base64图片
        handleClickLogo: () => { //处理LOGO点击事件
        }
      },
      menuBar: {
        //菜单栏样式
        style: {
          background: 'hsla(215, 100%, 65%, 1)',
        },
        //切换语言按钮
        languageButton:{
          show: false, //是否显示
          defaultLanguage: 'zh-cn' //默认语言 en zh-cn zh-tw
        },
        //新建按钮
        newButton:{
          show: true, //是否显示
          handleBefore(){
            //拦截点击事件，返回true继续执行
            return true
          }
        },
        //从计算机加载按钮
        loadFileButton:{
          show: true, //是否显示
          handleBefore(){
            //拦截点击事件，返回true继续执行
            return true
          }
        },
        //保存到计算机按钮
        saveFileButton:{
          show: true, //是否显示
          handleBefore(){
            //拦截点击事件，返回true继续执行
            return true
          }
        },
        //加速模式按钮
        turboModeButton:{
          show: true //是否显示
        },
        //教程按钮
        helpButton:{
          show: true, //是否显示
          handleBefore:()=>{
            //拦截点击事件，返回true继续执行
            return false
          }
        },
        //我的物品按钮
        myStuff:{
          show: true, //是否显示
          url: '/myproject' //跳转的连接
        },
        //用户头像按钮
        userAvatar:{
          show: true, //是否显示
          username: username_dis, //用户名
          avatar: '../static/scratch/static/avatar.png', //用户头像
          handleClick(){
            //点击头像，可以弹出登录框等操作
          }
        },
        //自定义按钮
        customButtons: [
          {
            show: is_share, //是否显示
            buttonName: '分享', //按钮名
            style:{ //按钮样式
              color: 'white',
              background: 'hsla(30, 100%, 55%, 1)',
            },
            handleClick:()=>{ //按钮事件
              $.post("/share/"+project_id,
              {
                is_share:1,
              },
              function(data,status){
                window.location.href="../project/"+project_id; 
              });
            }
          },

          {
            show: true, //是否显示
            buttonName: '转到设计页面', //按钮名
            style:{ //按钮样式
              color: 'white',
              background: 'hsla(212, 56%, 48%, 1)',
            },
            handleClick:()=>{ //按钮事件
              window.location.href="../project/"+project_id; 
            }
          },

          {
            show: is_author, //是否显示
            buttonName: '保存作品', //按钮名
            style:{ //按钮样式
              color: 'white',
              //background: 'hsla(212, 56%, 48%, 1)',
              background: 'hsla(215, 100%, 65%, 1)',
              
            },
            handleClick:()=>{ //按钮事件
              $('#is_upload_Modal').modal('show')
              projectCover = window.scratch.getProjectCover(function(data,status){})
              projectName = window.scratch.getProjectName(function(data,status){})

              window.scratch.getProjectFile(file => {
                window.scratch.getProjectCover(cover => {
                  var data = new FormData()
                  data.append('project_file', file, 'project_file.sb3')
                  data.append('project_cover', cover)
                  data.append('project_name', projectName)

                  console.info(data)
                  fetch("/save_project/"+project_id,{
                    method: 'POST',
                    body: data
                  }).then(res => res.text()).then(text => {
                    setTimeout(() => {
                      $('#is_upload_Modal').modal('hide')
                      if (text == '0'){
                        $('#fail').modal('show')
                      }
                    }, 1000)
                  }).catch((error) => {
                    console.log('Error:', error);
                    $('#fail').modal('show')
                    $('#is_upload_Modal').modal('hide')
                  });

                })

              })

            }
          },
        ]
      }, 
      blocks:{
         // 积木缩放比例
        scale: 0.8,
        // 隐藏分类 费雷见README附录：
        // 如需动态隐藏显示分类或积木，修改此配置后需手动执行 window.vm.emitWorkspaceUpdate()
        hideCatagorys:[], 
        //隐藏积木 积木代码见README附录：
        hideBlocks:[],
      },
      stageArea:{ //舞台设置
        fullscreenButton:{ //全屏按钮
          show: true,
          handleBeforeSetStageUnFull(){ //拦截退出全屏，返回true继续执行
            return true
          },
          handleBeforeSetStageFull(){ //拦截全屏，返回true继续执行
            return true
          }
        },
        startButton:{ //开始按钮
          show: true,
          handleBeforeStart(){ //拦截开始按钮，返回true继续执行
            return true
          }
        },
        stopButton:{ // 停止按钮
          show: true,
          handleBeforeStop(){ //拦截停止按钮，，返回true继续执行
            return true
          }
        }
      },
      //scratch vm初始化完毕
      handleVmInitialized: (vm) => {
        window.vm = vm;
        //vm.loadProject("static/scratch/static/project/" + project_id.toString() +".sb3")

      },
      //作品加载完毕
      handleProjectLoaded:() => {
        
        window.scratch.setProjectName(project_name)
      },
      //默认作品加载完毕
      handleDefaultProjectLoaded:() => {
        window.scratch.setProjectName(project_name)
      },
      //默认项目地址,不需要修请删除本配置项
      defaultProjectURL: "../static/upload/project/" + project_id.toString() +".sb3",
      //素材库配置

      assets:{
        //素材库地址，默认为/static下的素材库
        assetHost: '../static/scratch/static',
        //素材库索引地址
        defaultIndex:{
          sprites: "/static/scratch/static/json_index/sprites.json",
          costumes: "/static/scratch/static/json_index/costumes.json",
          backdrops: "/static/scratch/static/json_index/backdrops.json",
          sounds: "/static/scratch/static/json_index/sounds.json"
        },
        //拦截角色库打开
        handleBeforeSpriteLibraryOpen(){
          console.log("角色库打开")
          //追加素材库
          // window.scratch.pushSpriteLibrary(Arrays)
          return true;
        },
        //拦截造型库打开
        handleBeforeCostumesLibraryOpen(){
          return true;
        },
        //拦截背景库打开
        handleBeforeBackdropsLibraryOpen(){
          return true;
        },
        //拦截声音库打开
        handleBeforeSoundLibraryOpen(){
          return true;
        }
      },
    }


  </script>
</head>

<body>

  <div class="modal fade" id="is_upload_Modal" tabindex="-1" aria-labelledby="is_upload_Modal" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-body">
          上传中...
        </div>
      </div>
    </div>
  </div>

  <div class="modal fade" id="fail" tabindex="-1" aria-labelledby="fail" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">上传失败</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          网络错误或您没有权限修改
        </div>
      </div>
    </div>
  </div>


  <div id="scratch">
    <p>请稍候</p>
  </div>
<script type="text/javascript" src="/static/scratch/lib.min.js"></script><script type="text/javascript" src="/static/scratch/chunks/gui.js"></script></body>

<style>
  .custom-button_custom-button_lWaP6{
    margin-left: 0;
    border-radius: 5px;
    transition: box-shadow 0.5s;
  }
  .custom-button_custom-button_lWaP6:hover{
    box-shadow: 0px 5px 8px #3c6e97;
    margin-bottom: 10;
  }
</style>
</html>
